using logging;

interface Iterator{
    // object getFirst();
    // object getNext();
    // bool hasNext();
    bool next(out object item);
}

class GeneralIterator: Iterator{
    private AbstractCollection _data;
    public GeneralIterator(AbstractCollection data){
        this._data = data;
    }

    private int iter_index = 0;
    public bool next(out object item){  // 集成了 hasNext() 方法
        if(iter_index < _data.size()){  // here use a length of data.
            item = _data.item(iter_index);
            iter_index += 1;
            return true;
        }else{
            iter_index = 0;
            item = null;
            return false;
        }
    }
}

abstract class AbstractCollection{
    public abstract int size();
    public abstract object item(int index);
    public GeneralIterator iterator(){
        return new GeneralIterator(this);
    }
}

// 新建一个容器类，可以很轻易的获得迭代器接口
class MyCollection: AbstractCollection{
    private object[] _data = {123, 3.1415926, "中华民族"};

    public override int size(){ return _data.Length; }
    public override object item(int index){ return _data[index]; }
}

class Demo{
    static void Main(){
        var list = new MyCollection();
        var iter = list.iterator();

        for(object item; iter.next(out item);){  // like this: foreach(var item in list)
            Log.print($"Here is the item: {item}");
        }
    }
}
